const Base = require("../base.js");
/*
换位置
databaseName =  数据库名称
id=  当前记录的id
zt =  1 和上一条记录换  2为下一条换
*/
module.exports = class extends Base {
  async indexAction() {
    const databaseName = this.get("databaseName");
    const id = this.get("id");
    const zt = this.get("zt"); //排序的方式 1 和上一条记录换  2为下一条换
    const dataBase = this.model(databaseName,this.get("tb"));
    const arrList = await dataBase.field("id,paixu").order("paixu DESC").select();
    const xb = this.getsubscript(arrList, id);

    //     和上一条记录换位置
    let daqee = {};
    if (zt == 1) {
      if (xb == 0) {
        return this.fail("已是最前面的一条的记录");
      }
      daqee = arrList[xb - 1];
    }
    if (zt == 2) {
      if (xb == arrList.length - 1) {
        return this.fail("已是最前后的一条的记录");
      }
      daqee = arrList[xb + 1];
    }

    await dataBase.where({ id }).update({ paixu: daqee.paixu });
    await dataBase.where({ id: daqee.id }).update({ paixu: arrList[xb].paixu });
    this.success("操作成功！");
  }
  //   获取下标
  getsubscript(data, id) {
    let xiabnai = 0;
    data.map((a, b) => {
      if (a.id == id) {
        xiabnai = b;
      }
    });
    return xiabnai;
  }
};
